
package com.scs.application.modules.upms.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.SystemProperties;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.RestResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.BeanUtils;
import com.scs.application.core.utils.Md5Utils;
import com.scs.application.core.utils.UtilCommon;
import com.scs.application.modules.msg.service.PushedRecordService;
import com.scs.application.modules.order.entity.PurchaseOrder;
import com.scs.application.modules.order.service.PurchaseOrderService;
import com.scs.application.modules.upms.dto.UsernameAndPasswordLoginDTO;
import com.scs.application.modules.upms.dto.vx.VxServerConfig;
import com.scs.application.modules.upms.dto.vx.VxServerPushConfig;
import com.scs.application.modules.upms.entity.User;
import com.scs.application.modules.upms.entity.UserWx;
import com.scs.application.modules.upms.service.UserService;
import com.scs.application.modules.upms.service.UserWxService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
* 行政区划 controller
*
*/
@AllArgsConstructor
@RestController
@RequestMapping("/base/userwx")
public class UserWxController extends CrudController<UserWx,String, UserWxService>{
    private final SystemProperties systemProperties;
    private final PurchaseOrderService purchaseOrderService;
    private final PushedRecordService pushedRecordService;
    private final UserService userService;

    /***
     * @Description:  获取服务号配置信息
     */
    @GetMapping("/getServerConfig")
    public RestResult getServerConfig() {
        VxServerConfig vxServerConfig = new VxServerConfig();
        BeanUtils.copyProperties(systemProperties, vxServerConfig);
        vxServerConfig.setPushConfigList(new ArrayList<>());
        vxServerConfig.getPushConfigList().add(VxServerConfig.poPass);
        vxServerConfig.getPushConfigList().add(VxServerConfig.poIn);
        vxServerConfig.getPushConfigList().add(VxServerConfig.busCommon);
        return RestResult.ok("获取数据成功",vxServerConfig);
    }

    /***
     * @Description:  测试推送消息
     */
    @PostMapping("/testPush")
    public RestResult testPush(@RequestBody VxServerPushConfig vxServerPushConfig) {
        PurchaseOrder order = purchaseOrderService.getOne(Wrappers.<PurchaseOrder>query().inSql("supplier_id","select id from supplier").orderByDesc("gmt_create").last("limit 1"));
        if (order == null) {
            return RestResult.error("目前系统中没有任何采购订单,无法测试");
        }
        pushedRecordService.pushVxPoPassAsync(order,true);
        return RestResult.ok("操作成功");
    }

    /***
     * @Description:  微信配置
     */
    @GetMapping("/getWxConfig")
    public RestResult getWxConfig(String url) {
        return RestResult.ok("操作成功",pushedRecordService.getWxConfig(url));
    }

    /***
     * @Description:  微信配置
     */
    @GetMapping("/getUserByOpenIdOrLoginKey")
    public RestResult getUserByOpenIdOrLoginKey(String openId,String loginKey) {
        return RestResult.ok("操作成功",service.getUserByOpenIdOrLoginKey(openId, UtilCommon.getStr(loginKey,"")));
    }

    /**
     * 绑定账户
     * @param dto
     * @return
     */
    @PostMapping("/bind")
    public CommonResult bind(@RequestBody  UsernameAndPasswordLoginDTO dto) {

        User user = userService.findByLoginKey(dto.getUsername());
        if (user == null) {
            return CommonResult.error("绑定失败，未找到相关用户");
        }

        if (!dto.getPassword().equals(Md5Utils.md5(user.getPassword()))
                && !dto.getPassword().equals(user.getPassword())
                && !Md5Utils.md5(dto.getPassword()).equals(user.getPassword())

        ) {
            return CommonResult.error("绑定失败，账户密码输入错误");
        }
        synchronized (dto.getOpenId().intern()) {
            UserWx userWxGet = service.getOne(Wrappers.<UserWx>query().eq("login_key", dto.getUsername()).eq("open_id", dto.getOpenId()), false);
            if (userWxGet != null) {
                return CommonResult.error("该账户已绑定当前微信，请勿重复操作");
            }
            service.bind(dto.getUsername(),dto.getOpenId());
            // 设置为微信默认登录账号
            if (dto.getFlagDefaultVxLogin() != null && dto.getFlagDefaultVxLogin()) {
                UserWx userWx = new UserWx();
                userWx.setOpenId(dto.getOpenId());
                userWx.setLoginKey(dto.getUsername());
                service.setDefaultLogin(userWx);
            }
        }


        return CommonResult.ok("绑定成功");
    }

    /**
     * 设置默认登录账号
     * @param dto
     * @return
     */
    @PostMapping("/setDefaultLogin")
    public CommonResult setDefaultLogin(@RequestBody  UserWx dto) {
        service.setDefaultLogin(dto);
        return CommonResult.ok("绑定成功");
    }
}
